package net.ustc.javamemcached.memorystore;

import java.util.Set;
import java.util.Map.Entry;

import net.ustc.javamemcached.Key;
import net.ustc.javamemcached.util.SizedItem;

public class MemoryStoreManagerImpl implements IMemoryManager {
	private long memorySize;
	private MemoryBucketManagerImpl[] bucketManagers;
	private long bucketSize;
	private int blockSize;
	
	private class MapSizedItem implements SizedItem{
		long index;
		long size;
		public long size(){
			return size;
		}
	}
	
	// equals to usedBlockNum*blockSize
	private long usedByte;
	
	public MemoryStoreManagerImpl(long memorySize, long bucketSize, int blockSize){
		this.memorySize = memorySize;
		this.bucketSize = bucketSize;
		this.blockSize = blockSize;
		// init every bucket
		bucketManagers = new MemoryBucketManagerImpl[(int)(memorySize/bucketSize)];
		for(MemoryBucketManagerImpl bucketManager : bucketManagers){
			bucketManager = new MemoryBucketManagerImpl(bucketSize, blockSize);
		}
	}
	
	
	public boolean add(Key key, MemoryEntry entry) {
//		boolean isExist = get(Key);
		return false;
	}


	public void clear() {
		// TODO Auto-generated method stub
		
	}


	public boolean contain(Key key) {
		// TODO Auto-generated method stub
		return false;
	}


	public Set<Entry<Key, MemoryEntry>> entrySet() {
		// TODO Auto-generated method stub
		return null;
	}


	public MemoryEntry get(Key key) {
		// TODO Auto-generated method stub
		return null;
	}


	public long getMemoryCapacity() {
		// TODO Auto-generated method stub
		return 0;
	}


	public long getUsedSize() {
		// TODO Auto-generated method stub
		return 0;
	}


	public Set<Key> keySet() {
		// TODO Auto-generated method stub
		return null;
	}


	public boolean remove(Key key) {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean set(Key key, MemoryEntry entry) {
		// TODO Auto-generated method stub
		return false;
	}

	public Set<MemoryEntry> values() {
		// TODO Auto-generated method stub
		return null;
	}

	public void close(){
		for(MemoryBucketManagerImpl bucketManager : bucketManagers){
			bucketManager.close();
		}
	}

	/**
     * Rounds up a requested size to the nearest block width.
     * @param size the requested size
     * @param blockSize the block size to use
     * @return the actual mount to use
     */
    public static long roundUp( long size, long blockSize ) {
        return size - 1L + blockSize - (size - 1L) % blockSize;
    }
    
    protected static int hash(int h) {
        // Spread bits to regularize both segment and index locations,
        // using variant of single-word Wang/Jenkins hash.
        h += (h <<  15) ^ 0xffffcd7d;
        h ^= (h >>> 10);
        h += (h <<   3);
        h ^= (h >>>  6);
        h += (h <<   2) + (h << 14);
        return h ^ (h >>> 16);
    }
}
